<template>
  <div class="app-container">
     <el-row :gutter="20">
       <!--部门数据-->
       <el-col :span="4" :xs="24">
         <div class="head-container">
           <el-input
             v-model="typeName"
             placeholder="请输入类型名称"
             clearable
             size="small"
             prefix-icon="el-icon-search"
             style="margin-bottom: 20px"
           />
         </div>
         <div class="head-container">
           <el-tree
             :data="typeOptions"
             :props="defaultProps"
             :expand-on-click-node="false"
             :filter-node-method="filterNode"
             ref="tree"
             default-expand-all
             @node-click="handleNodeClick"
           />
         </div>
       </el-col>
       <!--用户数据-->
       <el-col :span="20" :xs="24">
         <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
           <el-form-item label="名称" prop="name">
             <el-input
               v-model="queryParams.userName"
               placeholder="请输入用户名称"
               clearable
               size="small"
               style="width: 240px"
               @keyup.enter.native="handleQuery"
             />
           </el-form-item>
           <el-form-item label="状态" prop="status">
             <el-select
               v-model="queryParams.status"
               placeholder="状态"
               clearable
               size="small"
               style="width: 240px"
             >
               <el-option
                 v-for="dict in dict.type.sys_normal_disable"
                 :key="dict.value"
                 :label="dict.label"
                 :value="dict.value"
               />
             </el-select>
           </el-form-item>
           <el-form-item>
             <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
             <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
           </el-form-item>
         </el-form>

         <el-row :gutter="10" class="mb8">
           <el-col :span="1.5">
             <el-button
               type="primary"
               plain
               icon="el-icon-plus"
               size="mini"
               @click="handleAdd"
               v-hasPermi="['system:sysCheckItem:add']"
             >新增</el-button>
           </el-col>
           <el-col :span="1.5">
             <el-button
               type="success"
               plain
               icon="el-icon-edit"
               size="mini"
               :disabled="single"
               @click="handleUpdate"
               v-hasPermi="['system:sysCheckItem:edit']"
             >修改</el-button>
           </el-col>
           <el-col :span="1.5">
             <el-button
               type="danger"
               plain
               icon="el-icon-delete"
               size="mini"
               :disabled="multiple"
               @click="handleDelete"
               v-hasPermi="['system:sysCheckItem:remove']"
             >删除</el-button>
           </el-col>
           <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
         </el-row>

         <el-table v-loading="loading" :data="sysCheckItemList" @selection-change="handleSelectionChange">
           <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="名称" align="center" prop="name" />
           <el-table-column label="类型" align="center" prop="typeName" />
           <el-table-column label="排序" align="center" prop="sort" />
           <el-table-column label="值类型" align="center" prop="valueType">
             <template slot-scope="scope">
               <dict-tag :options="dict.type.value_type" :value="scope.row.valueType"/>
             </template>
           </el-table-column>
           <el-table-column label="展示类型" align="center" prop="htmlType">
             <template slot-scope="scope">
               <dict-tag :options="dict.type.html_type" :value="scope.row.htmlType"/>
             </template>
           </el-table-column>
           <!-- <el-table-column label="值单位" align="center" prop="valueUnit" /> -->
           <!-- <el-table-column label="缺省值" align="center" prop="defaultValue" /> -->
           <el-table-column label="状态" align="center" prop="status">
             <template slot-scope="scope">
               <el-switch
                 v-model="scope.row.status"
                 active-value="0"
                 inactive-value="1"
                 @change="handleStatusChange(scope.row)"
               ></el-switch>
             </template>
           </el-table-column>
           <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
           <el-table-column label="项类型" align="center" prop="itemType">
             <template slot-scope="scope">
               <dict-tag :options="dict.type.item_type" :value="scope.row.itemType"/>
             </template>
           </el-table-column>
           <el-table-column label="重大隐患" align="center" prop="isMajor">
             <template slot-scope="scope">
                 <dict-tag :options="dict.type.is_major" :value="scope.row.isMajor"/>
               </template>
             </el-table-column>
           <el-table-column label="选择项" align="center" prop="selectText" />
           <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120" >
             <template slot-scope="scope">
               <el-button
                 size="mini"
                 type="text"
                 icon="el-icon-edit"
                 @click="handleUpdate(scope.row)"
                 v-hasPermi="['system:sysCheckItem:edit']"
               >修改</el-button>
               <el-button
                 size="mini"
                 type="text"
                 icon="el-icon-delete"
                 @click="handleDelete(scope.row)"
                 v-hasPermi="['system:sysCheckItem:remove']"
               >删除</el-button>
             </template>
           </el-table-column>
         </el-table>

         <pagination
           v-show="total>0"
           :total="total"
           :page.sync="queryParams.pageNum"
           :limit.sync="queryParams.pageSize"
           @pagination="getList"
         />
       </el-col>
     </el-row>

    <!-- 添加或修改表单项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="880px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">

        <el-row :gutter="15">
          <el-col :span="8">
            <el-form-item label="表单项名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入表单项名称" />
            </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="所属类型" prop="typeId">
            <treeselect v-model="form.typeId" :options="typeTreeOptions" :normalizer="normalizer" placeholder="选择类型" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" />
          </el-form-item>
        </el-col>
        </el-row>

    <el-row :gutter="15">
    <el-col :span="8">
    <el-form-item label="值类型" prop="valueType">
      <el-select v-model="form.valueType" placeholder="请选择值类型">
        <el-option
          v-for="dict in dict.type.value_type"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        ></el-option>
      </el-select>
    </el-form-item>
    </el-col>

        <el-col :span="8">
        <el-form-item label="页面展示类型" prop="htmlType">
          <el-select v-model="form.htmlType" placeholder="请选择页面展示类型">
            <el-option
              v-for="dict in dict.type.html_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
          <el-col :span="8">
            <el-form-item label="值单位" prop="valueUnit">
              <el-input v-model="form.valueUnit" placeholder="请输入值单位" />
            </el-form-item>
          </el-col>
        </el-row>
    <el-row :gutter="15" v-if="form.htmlType == 'radio' || form.htmlType == 'select'">
      <el-col :span="24">
        <el-form-item label="选择项" prop="selectText">
          <el-input v-model="form.selectText" type="textarea" placeholder="请输入选择项用#分割" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="15">
      <el-col :span="8">
        <el-form-item label="缺省值" prop="defaultValue">
          <el-input v-model="form.defaultValue" placeholder="请输入缺省值" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
      <el-form-item label="巡查项类型" prop="itemType">
        <el-select v-model="form.itemType" placeholder="请选择巡查项类型">
          <el-option
            v-for="dict in dict.type.item_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>

    <el-col :span="8">
      <el-form-item label="重大隐患" prop="isMajor">
        <el-select v-model="form.isMajor" placeholder="请选择是或者否">
          <el-option
            v-for="dict in dict.type.is_major"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-col>
        </el-row>

    <el-row :gutter="15">
      <el-col :span="8">
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.glo_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
        </el-row>
   <el-row :gutter="15">
     <el-col :span="24">
      <el-form-item label="关联标签" prop="tagIds">
        <el-select v-model="form.tagIds" multiple placeholder="请选择标签" style="width: 100%;">
          <el-option
            v-for="item in tagOptions"
            :key="item.tagId"
            :label="item.name"
            :value="item.tagId"
            :disabled="item.status == 1"
          ></el-option>
        </el-select>
      </el-form-item>
      </el-col>
    </el-row>
    <el-row :gutter="15">
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
        </el-col>
      </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
  </div>
</template>

<script>
import { listSysCheckItem, getSysCheckItem, delSysCheckItem, addSysCheckItem, updateSysCheckItem,changeItemStatus } from "@/api/system/sysCheckItem";
import { listSysCheckType,treeselect } from "@/api/system/sysCheckType";
import { options } from "@/api/system/sysTag";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "SysCheckItem",
  dicts: ['item_type', 'html_type', 'value_type', 'glo_status','is_major'],
  components: { Treeselect },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表单项表格数据
      sysCheckItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        typeId: undefined,
        valueType: undefined,
        htmlType: undefined,
        status: undefined,
        itemType: undefined,
        isMajor: undefined,
      },
      typeOptions:[],
      typeTreeOptions:[],
      typeName:undefined,
      tagOptions:[],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    typeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTypeTree()
  },
  methods: {
    /** 查询表单项列表 */
    getList() {
      this.loading = true;
      listSysCheckItem(this.queryParams).then(response => {
        this.sysCheckItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTypeTree(){
      treeselect().then(response => {
        this.typeOptions = response.data
      });
    },
    /** 转换类型数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.typeId,
        label: node.name,
        children: node.children
      };
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeId = data.id;
      this.handleQuery();
    },
    getTagOptions() {
      options().then(response => {
        this.tagOptions = response.data;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: undefined,
        name: undefined,
        typeId: undefined,
        sort: undefined,
        valueType: undefined,
        htmlType: undefined,
        valueUnit: undefined,
        defaultValue: undefined,
        dictType: undefined,
        status: "0",
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        itemType: undefined,
        isMajor: undefined,
        hisStr: undefined,
        selectText: undefined,
        tagIds:[]
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加表单项";
      this.getTagOptions();
      listSysCheckType().then(response => {
        this.typeTreeOptions = this.handleTree(response.data, "typeId");
      });
      this.form.typeId = this.queryParams.typeId;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.getTagOptions();
      listSysCheckType().then(response => {
        this.typeTreeOptions = this.handleTree(response.data, "typeId");
      });
      const itemId = row.itemId || this.ids
      getSysCheckItem(itemId).then(response => {
        this.loading = false;
        this.form = response.data.item;
        this.form.tagIds = response.data.tagIds;
        this.open = true;
        this.title = "修改表单项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.itemId != null) {
            updateSysCheckItem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSysCheckItem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"吗？').then(function() {
        return changeItemStatus(row.itemId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$modal.confirm('是否确认删除表单项编号为"' + itemIds + '"的数据项？').then(() => {
        this.loading = true;
        return delSysCheckItem(itemIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/sysCheckItem/export', {
        ...this.queryParams
      }, `sysCheckItem_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
